using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Hardware;
namespace HouseControl
{
    public class Lights : Application
    {
        /// <summary>
        /// function that changes the state of the light in Room 1 when a button is pressed on the Tahoe 2
        /// </summary>
        /// <param name="data1">Default Parameter of a Native Event Handler </param>
        public static void light_1(uint data1, uint data2, DateTime date)
        {

            if (data2 == 1)  //to know if the button pressed is released or not 
            {
                Program.stateLed = !Program.stateLed;  // change light status 
                Program.led.Write(Program.stateLed);  //write it to the Led
              
            }

        }

        /// <summary>
        /// function that changes the state of the light in Room 2 when a button is pressed on the Tahoe 2
        /// </summary>
        /// <param name="data1">Default Parameter of a Native Event Handler </param>
        public static void light_2(uint data1, uint data2, DateTime time)
        {

            if (data2 == 1) //to know if the button pressed is released or not 
            {
                Program.stateLeds = !Program.stateLeds;  // change light status 
                Program.leds.Write(Program.stateLeds);   //write it to the Led
                
            }
        }

        public static void CheckingLightsPageIndex(object sender, EventArgs evt)  //index that we use to access to a Page or to leave it
        {
            if (Program.firstroomSet != null)   //if the page already exists 
            {
                Program.firstroomSet.Close();
                Program.firstroomSet = null;
                Program.CheckingsPage();
            }
            else if (Program.firstroomSet == null)
            {
                Program.mainWindow.Close();
                Program.mainWindow = null;
                CheckingLightsPage();
            }

        }

        public static Window CheckingLightsPage()
        {
            Program.firstroomSet = new Window();
            Program.firstroomSet.Height = SystemMetrics.ScreenHeight;
            Program.firstroomSet.Width = SystemMetrics.ScreenWidth;
            StackPanel stackPanel = new StackPanel();
           
                //display items of the page
                Image room1 = new Image(Resources.GetBitmap(Resources.BitmapResources.room11));
                SimpleButton room1Button = new SimpleButton(room1, Program.firstroomSet.Width, 100);
                room1Button.Click += new EventHandler(FirstroomlightPageIndex);

                Image room2 = new Image(Resources.GetBitmap(Resources.BitmapResources.room21));
                SimpleButton room2Button = new SimpleButton(room2, Program.firstroomSet.Width, 110);
                room2Button.Click += new EventHandler(SecondroomlightPageIndex);

                Image back = new Image(Resources.GetBitmap(Resources.BitmapResources.back2));
                SimpleButton backButton = new SimpleButton(back, Program.firstroomSet.Width, 40);
                backButton.Click += new EventHandler(CheckingLightsPageIndex);

                stackPanel.Children.Add(room1Button);
                stackPanel.Children.Add(room2Button);
                stackPanel.Children.Add(backButton);
            


            Program.firstroomSet.Child = stackPanel;
            stackPanel.SetMargin(0, 0, 00, 0);
            Program.firstroomSet.Visibility = Visibility.Visible;


            return Program.firstroomSet;
        }

        public static void FirstroomlightPageIndex(object sender, EventArgs evt)  //index that we use to access to a Page or to leave it
        {
            if (Program.firstroomlightSet != null)  //if already exists
            {
                Program.firstroomlightSet.Close();
                Program.firstroomlightSet = null;
                CheckingLightsPage();
            }
            else if (Program.firstroomlightSet == null)
            {
                Program.firstroomSet.Close();
                Program.firstroomSet = null;
                FirstroomlightPage();
            }

        }

        public static void SecondroomlightPageIndex(object sender, EventArgs evt)  //index that we use to access to a Page or to leave it
        {
            if (Program.secondroomlightSet != null)
            {
                Program.secondroomlightSet.Close();
                Program.secondroomlightSet = null;
                CheckingLightsPage();
            }
            else if (Program.secondroomlightSet == null)
            {
                Program.firstroomSet.Close();
                Program.firstroomSet = null;
                SecondroomlightPage();
            }

        }

        public static void RefreshSecondroomlightPageIndex(object sender, EventArgs evt)  //index that we use to access to a Page or to leave it
        {
            if (Program.secondroomlightSet != null)
            {
                Program.secondroomlightSet.Close();

                SecondroomlightPage();
            }
        }

        public static void RefreshFirstroomlightPageIndex(object sender, EventArgs evt)  //index that we use to access to a Page or to leave it
        {
            if (Program.firstroomlightSet != null)
            {
                Program.firstroomlightSet.Close();
                Program.firstroomlightSet = null;
                FirstroomlightPage();
               
            }


        }

        public static Window FirstroomlightPage()
        {

            Program.firstroomlightSet = new Window();
            Program.firstroomlightSet.Height = SystemMetrics.ScreenHeight;
            Program.firstroomlightSet.Width = SystemMetrics.ScreenWidth;
            StackPanel stackPanel = new StackPanel();

            //change the light in room 1 if someone in that room change it. This is to Test thje Refresh Button and all the interactions between all the users, we test it by using SW1
            Program.port.OnInterrupt += new NativeEventHandler(light_1);  
            
           
                if (Program.stateLed == false)
                {

                    Image lightoff1 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledoffroom11));
                    SimpleButton lightoff1Button = new SimpleButton(lightoff1, Program.firstroomlightSet.Width, 100);
                    lightoff1Button.Click += new EventHandler(lighton1displayPageIndex);
                    stackPanel.Children.Add(lightoff1Button);

                }
                else if (Program.stateLed == true)
                {

                    Image lighton1 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledonroom11));
                    SimpleButton lighton1Button = new SimpleButton(lighton1, Program.firstroomlightSet.Width, 100);
                    lighton1Button.Click += new EventHandler(lighton1displayPageIndex);
                    stackPanel.Children.Add(lighton1Button);
                }

                Image refresh = new Image(Resources.GetBitmap(Resources.BitmapResources.refresh11));
                SimpleButton refreshButton = new SimpleButton(refresh, Program.firstroomlightSet.Width, 37);
                refreshButton.Click += new EventHandler(RefreshFirstroomlightPageIndex);
                stackPanel.Children.Add(refreshButton);

                Image back = new Image(Resources.GetBitmap(Resources.BitmapResources.back2));
                SimpleButton backButton = new SimpleButton(back, Program.firstroomlightSet.Width, 40);
                backButton.Click += new EventHandler(FirstroomlightPageIndex);
                stackPanel.Children.Add(backButton);
            

            

            Program.firstroomlightSet.Child = stackPanel;
            stackPanel.SetMargin(0, 0, 00, 0);
            Program.firstroomlightSet.Visibility = Visibility.Visible;
            return Program.firstroomlightSet;
        }

        public static Window SecondroomlightPage()
        {

            Program.secondroomlightSet = new Window();
            Program.secondroomlightSet.Height = SystemMetrics.ScreenHeight;
            Program.secondroomlightSet.Width = SystemMetrics.ScreenWidth;
            StackPanel stackPanel = new StackPanel();

            //if a user in Room 2 change the light or not this is to test interaction between users, we test it here by using SW2
            Program.ports.OnInterrupt += new NativeEventHandler(Lights.light_2); 
                  
           
                if (Program.stateLeds == false)
                {

                    Image lightoff2 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledoffroom21));
                    SimpleButton lightoff2Button = new SimpleButton(lightoff2, Program.secondroomlightSet.Width, 100);
                    //when the button is pressed then we go to another page and the status of the light is automatically changed
                    lightoff2Button.Click += new EventHandler(lighton2displayPageIndex);
                    stackPanel.Children.Add(lightoff2Button);

                }
                else if (Program.stateLeds == true)
                {

                    Image lighton2 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledonroom21));
                    SimpleButton lighton2Button = new SimpleButton(lighton2, Program.secondroomlightSet.Width, 100);
                    //when the button is pressed then we go to another page and the status of the light is automatically changed
                    lighton2Button.Click += new EventHandler(lighton2displayPageIndex);
                    stackPanel.Children.Add(lighton2Button);
                }

                Image refresh = new Image(Resources.GetBitmap(Resources.BitmapResources.refresh11));
                SimpleButton refreshButton = new SimpleButton(refresh, Program.secondroomlightSet.Width, 37);//refresh
                refreshButton.Click += new EventHandler(RefreshSecondroomlightPageIndex);
                stackPanel.Children.Add(refreshButton);

                Image back = new Image(Resources.GetBitmap(Resources.BitmapResources.back2));
                SimpleButton backButton = new SimpleButton(back, Program.secondroomlightSet.Width, 40);
                backButton.Click += new EventHandler(SecondroomlightPageIndex);
                stackPanel.Children.Add(backButton);
            
            

            Program.secondroomlightSet.Child = stackPanel;
            stackPanel.SetMargin(0, 0, 00, 0);
            Program.secondroomlightSet.Visibility = Visibility.Visible;
            return Program.secondroomlightSet;
        }

        public static void lighton1displayPageIndex(object sender, EventArgs evt) //index 
        {
            if (Program.lighton1Set != null)
            {
                Program.lighton1Set.Close();
                Program.lighton1Set = null;
                Program.firstroomlightSet = null;
                CheckingLightsPage();
            }
            else if (Program.lighton1Set == null)
            {
                Program.firstroomlightSet.Close();
                Program.firstroomlightSet = null;
                lighton1displayPage();
            }

        }

        public static void lighton2displayPageIndex(object sender, EventArgs evt) //index
        {
            if (Program.lighton2Set != null)
            {
                Program.lighton2Set.Close();
                Program.lighton2Set = null;

                Program.secondroomlightSet = null;
                CheckingLightsPage();
            }
            else if (Program.lighton2Set == null)
            {
                Program.secondroomlightSet.Close();
                Program.secondroomlightSet = null;
                lighton2displayPage();
            }

        }

        public static Window lighton1displayPage()
        {

            Program.lighton1Set = new Window();
            Program.lighton1Set.Height = SystemMetrics.ScreenHeight;
            Program.lighton1Set.Width = SystemMetrics.ScreenWidth;
            StackPanel stackPanel = new StackPanel();

            //change the light when we get in this page
            Program.stateLed = !Program.stateLed;  
            Program.led.Write(Program.stateLed);

         
                if (Program.stateLed == true) //status control
                {
                    Image lighton1 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledon11));
                    SimpleButton lighton1Button = new SimpleButton(lighton1, Program.lighton1Set.Width, 100);
                    stackPanel.Children.Add(lighton1Button);
                }

                else if (Program.stateLed == false) //status contol
                {
                    Image lighton1 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledoff11));
                    SimpleButton lighton1Button = new SimpleButton(lighton1, Program.lighton1Set.Width, 100);
                    stackPanel.Children.Add(lighton1Button);
                }

                if (Program.light_interaction == true) //interaction control
                {
                    Image interaction = new Image(Resources.GetBitmap(Resources.BitmapResources.notification));
                    SimpleButton interactionButton = new SimpleButton(interaction, Program.lighton1Set.Width, 40);
                    stackPanel.Children.Add(interactionButton);
                }
                Image back = new Image(Resources.GetBitmap(Resources.BitmapResources.back2));
                SimpleButton backButton = new SimpleButton(back, Program.lighton1Set.Width, 40);
                backButton.Click += new EventHandler(lighton1displayPageIndex);
                stackPanel.Children.Add(backButton);
            
            Program.lighton1Set.Child = stackPanel;
            stackPanel.SetMargin(0, 0, 00, 0);
            Program.lighton1Set.Visibility = Visibility.Visible;
            Program.light_interaction = false;
            return Program.lighton1Set;
        }

        public static Window lighton2displayPage() //same system as above
        {

            Program.lighton2Set = new Window();
            Program.lighton2Set.Height = SystemMetrics.ScreenHeight;
            Program.lighton2Set.Width = SystemMetrics.ScreenWidth;
            StackPanel stackPanel = new StackPanel();

            Program.stateLeds = !Program.stateLeds;
            Program.leds.Write(Program.stateLeds);

          
                if (Program.stateLeds == true)
                {
                    Image lighton2 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledon21));
                    SimpleButton lighton2Button = new SimpleButton(lighton2, Program.lighton2Set.Width, 100);
                    stackPanel.Children.Add(lighton2Button);
                }

                else if (Program.stateLeds == false)
                {
                    Image lighton2 = new Image(Resources.GetBitmap(Resources.BitmapResources.ledoff21));
                    SimpleButton lighton2Button = new SimpleButton(lighton2, Program.lighton2Set.Width, 100);
                    stackPanel.Children.Add(lighton2Button);
                }

                if (Program.light_interaction == true)
                {
                    Image interaction = new Image(Resources.GetBitmap(Resources.BitmapResources.notification));
                    SimpleButton interactionButton = new SimpleButton(interaction, Program.lighton2Set.Width, 40);
                    stackPanel.Children.Add(interactionButton);
                }

                Image back = new Image(Resources.GetBitmap(Resources.BitmapResources.back2));
                SimpleButton backButton = new SimpleButton(back, Program.lighton2Set.Width, 40);
                backButton.Click += new EventHandler(lighton2displayPageIndex);
                stackPanel.Children.Add(backButton);
            
            Program.lighton2Set.Child = stackPanel;
            stackPanel.SetMargin(0, 0, 00, 0);
            Program.lighton2Set.Visibility = Visibility.Visible;
            Program.light_interaction = false;
            return Program.lighton2Set;
        }

   }
}